//
//  XFHomeTableViewController.swift
//  SinaBlog
//
//  Created by macs on 15/11/8.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
let identifier = "reuseIdentifier"
class HomeTableViewController: BaseTableViewController {
    
    private lazy var statues = [Status]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //  在没有登录时不会走下面的 .bleview.如果不判断就会造成 nil 的错误
        if !isLogin {
            visitLoginView?.setSubViewsProperty(nil, descri: "关注一些人，回这里看看有什么惊喜")
            return
        }
        //注册 cell, 及其加载数据
        prepraretableViewcell()
    }
    
    func prepraretableViewcell(){
        
        //添加下拉刷新功能
        refreshControl = NewRefreshController()
        //添加刷新操作
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        //注册 cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: identifier)
        
        //加载刷新数据
        loadData()
        
        //自动计算行高.设置属性,设置自动布局
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
        //tableView.rowHeight = 100
        
        // tableView.separatorStyle = .None
        //cell 不被选中的两种方式之一
        //        tableView.allowsSelection = false
        tableView.tableFooterView = indicatorView
    }
    
    func loadData() {
        
        var since_id = 0
        var max_id = 0
        
        if indicatorView.isAnimating() {
            since_id = 0
            max_id = statues.last?.id ?? 0
        } else {
            max_id = 0
            since_id = statues.first?.id ?? 0
        }
        
        StatusViewModel().getStatues(since_id: since_id, max_id: max_id) { (array) -> () in
            self.refreshControl?.endRefreshing()
            
            //print(array?.count)
            if (array == nil) {
                
                SVProgressHUD.showErrorWithStatus(NetErrorTip)
                //print("错误")
                self.indicatorView.stopAnimating()
                return
            }
            
            if since_id > 0 {
                //下拉加在更多
                self.statues = array! + self.statues
            } else if max_id > 0 {
                //上啦加载更多
                self.statues += array!
                self.indicatorView.stopAnimating()
            } else {
                //首次加载数据
                self.statues += array!
            }
            
            
            self.tableView.reloadData()
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        
       
        SVProgressHUD.dismiss()
        super.viewDidAppear(animated)
    }
    
    
    override func viewDidDisappear(animated: Bool) {
        
         self.refreshControl?.endRefreshing()
        super.viewDidDisappear(animated);
    }
    
    
    private lazy var indicatorView : UIActivityIndicatorView = {
        let ind = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return ind
    }()
    
    
    
}

//数据源方法
extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statues.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! StatusCell
        
        //        cell.textLabel?.text = statues[indexPath.row].user?.name
        cell.status = statues[indexPath.row]
        
        //当走到倒数第二行时加载数据
        if indexPath.row == statues.count - 2 && !indicatorView.isAnimating() {
            
            indicatorView.startAnimating()
            loadData()
            //print("开始加载数据")
        }
        
        return cell
    }
    
}
